//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Controllers;
using DotNetNuke.Entities.Host;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Modules;
using PortalController = DotNetNuke.Entities.Portals.PortalController;

namespace DotNetNuke.Modules.Admin.Search
{
	partial class SearchAdmin : ModuleUserControlBase
	{
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdReIndex.Click += new EventHandler(cmdReIndex_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);

			if (!Page.IsPostBack) {
				if (this.ModuleContext.PortalSettings.ActiveTab.ParentId == ModuleContext.PortalSettings.SuperTabId) {
					txtMaxWordLength.Text = Host.SearchMaxWordlLength.ToString();
					txtMinWordLength.Text = Host.SearchMinWordlLength.ToString();
					chkIncludeCommon.Checked = Host.SearchIncludeCommon;
					chkIncludeNumeric.Checked = Host.SearchIncludeNumeric;
				} else {
					txtMaxWordLength.Text = ModuleContext.PortalSettings.SearchMaxWordlLength.ToString();
					txtMinWordLength.Text = ModuleContext.PortalSettings.SearchMinWordlLength.ToString();
					chkIncludeCommon.Checked = ModuleContext.PortalSettings.SearchIncludeCommon;
					chkIncludeNumeric.Checked = ModuleContext.PortalSettings.SearchIncludeNumeric;
				}
			}
		}
		private void cmdReIndex_Click(object sender, EventArgs e)
		{
			try {
				Services.Search.SearchEngine se = new Services.Search.SearchEngine();
				if (ModuleContext.PortalSettings.ActiveTab.ParentId == ModuleContext.PortalSettings.SuperTabId) {
					se.IndexContent();
				} else {
					se.IndexContent(ModuleContext.PortalId);
				}
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("Indexed", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try {
				if (ModuleContext.PortalSettings.ActiveTab.ParentId == ModuleContext.PortalSettings.SuperTabId) {
                    HostController.Instance.Update("MaxSearchWordLength", txtMaxWordLength.Text);
                    HostController.Instance.Update("MinSearchWordLength", txtMinWordLength.Text);
                    HostController.Instance.Update("SearchIncludeCommon", chkIncludeCommon.Checked ? "Y" : "N");
                    HostController.Instance.Update("SearchIncludeNumeric", chkIncludeNumeric.Checked ? "Y" : "N");
					DataCache.ClearHostCache(false);
				} else {
					PortalController.UpdatePortalSetting(ModuleContext.PortalId, "MaxSearchWordLength", txtMaxWordLength.Text);
					PortalController.UpdatePortalSetting(ModuleContext.PortalId, "MinSearchWordLength", txtMinWordLength.Text);
					PortalController.UpdatePortalSetting(ModuleContext.PortalId, "SearchIncludeCommon", chkIncludeCommon.Checked ? "Y" : "N");
					PortalController.UpdatePortalSetting(ModuleContext.PortalId, "SearchIncludeNumeric", chkIncludeNumeric.Checked ? "Y" : "N");
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
